<?php

/**
 * Description of AnimusFactory
 *
 * @author Animus Inc.
 * AnimusFactory is the principal API class, which will provide access to coreAPI classes 
 */

define('DOMROOT',dirname(dirname(dirname(__FILE__))));
define('CLASSPATH', DOMROOT.'/classes/');
define('APICORE',  CLASSPATH.'/APICore/');

require_once APICORE.'/AnimusSession.php';
require_once APICORE.'/AnimusDispatcher.php';
require_once APICORE.'/AnimusMVC.php';

class AnimusFactory {
    
    private $_AnimusRequest;
    private $_AnimusResponse;
    private $_AnimusDispatcher;
    private $_AnimusMVC;
    private $_AnimusBootstrap;


    public function __construct(AnimusRequest $Request, AnimusResponse $Response) {
        $this -> _AnimusRequest = $Request;
        $this -> _AnimusResponse = $Response;
        return;
    }
    public function loadBootstrapSettings(){
        $_comp_type = $this -> _AnimusMVC -> getComponentType();
        switch($_comp_type){
            case "admin": $this->_AnimusBootstrap->bootForAdminUser();
                break;
            case "user" : $this->_AnimusBootstrap->bootForFrontendUser();
        }
        var_dump($this->_AnimusBootstrap->getAdminMenuMap());
        return;
    }
    public function route(){
        $this -> _AnimusMVC = new AnimusMVC();
        $this -> _AnimusDispatcher = new AnimusDispatcher();
        $this -> _AnimusDispatcher ->setCurrentRequest($this->_AnimusRequest);
        $this -> _AnimusDispatcher -> lookup();
        $this -> _AnimusMVC ->modelViewLookup($this->_AnimusRequest);
        $this -> _AnimusMVC ->invokeInjectedComponent($this->_AnimusRequest, $this->_AnimusResponse);
        $this ->loadBootstrapSettings();
        return true;
    }
    public function startComponent(){
        $this ->injectSessionInRequest();
        $flag = $this->_AnimusMVC -> runComponent();
        if($flag){
            $this ->handleSessionOnResponse();
        }
        $this ->injectDataInView();
        return;
    }
    public function handleSessionOnResponse(){
        $response_validity = $this -> _AnimusResponse ->isValid();
        //Handle the response after initiating a fresh session
        if($response_validity){// Response is valid
            $session_instance = $this -> _AnimusResponse -> getSession();
            $user_info = $session_instance -> getUserInfo();
            if(!is_null($user_info)){ // If user info is not null, then requesting a new session
                $session_instance ->manageExistingSession();
            }
            else {
                // If user info is null, then close the session, since it is a logout request
                $session_instance ->closeSession ();
            }
           return;
        }
    }
    
    public function injectSessionInRequest(){
        $session_instance = new AnimusSession();
        $existing_session = $session_instance ->getSession();
        if(!is_null($existing_session)){
            $this -> _AnimusRequest ->setSession ($existing_session);
        }
        else{
            $this -> _AnimusRequest ->setSession ($session_instance);
        }
        return true;
    }
    
    public function injectDataInView(){
        $_view = $this -> _AnimusResponse -> getView();
        $_model = $this -> _AnimusResponse ->getModel();
        if(!is_null($_model)){ // This means that the component is not configured with a model
            $_view_data = $_model -> getViewData();
            $_view -> setViewData($_view_data);
        }
        else {};
    }
    
    public function getNewSession(){
        return new AnimusSession();
    }
    
    public  function setDispatcher(AnimusDispatcher $Dispatcher){
        $this->_AnimusDispatcher = $Dispatcher;
        return;
    }
    
    public function setRequest(AnimusRequest $Request){
        $this->_AnimusRequest = $Request;
        return true;
    }
    
    public function setResponse(AnimusResponse $Response){
        $this ->_AnimusResponse = $Response;
        return true;
    }

        public function getRequest(){
        return $this -> _AnimusRequest;
    }
    
    public function getResponse(){
        return $this -> _AnimusResponse;
    }
    public function getDispatcher(){
        return $this->_AnimusDispatcher;
    }
    public function getAnimusBootstrap() {
        return $this->_AnimusBootstrap;
    }

    public function setAnimusBootstrap($AnimusBootstrap) {
        $this->_AnimusBootstrap = $AnimusBootstrap;
    }
}
?>